package com.example.msai.service;

import com.example.msai.entity.Coordinate;
import com.example.msai.entity.DeepSeekRe;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service
public class MapUrlGeneratorService {

    private static final String BASE_URL = "https://restapi.amap.com/v3/staticmap";
    private static final String API_KEY = "31796c0b7beeca100b8e1b31c9bb63b6";

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private DeepSeekService deepSeekService;

    public String generateMapUrl(DeepSeekRe deepSeekRe) throws IOException {
        String q = deepSeekRe.getq();
        String res = deepSeekService.getAiResponse(q);
        String jsonCoordinates = res
                .replaceAll("^```json\\n", "")
                .replaceAll("\\n```$", "");
        System.out.println(jsonCoordinates);

        Map<String, Coordinate> coordinates = objectMapper.readValue(
                jsonCoordinates,
                new TypeReference<Map<String, Coordinate>>() {}
        );

        // 构建基础URL参数
        StringBuilder urlBuilder = new StringBuilder(BASE_URL)
                .append("?size=1280*1280")
                .append("&zoom=4")
                .append("&key=").append(API_KEY)
                .append("&location=").append("108.940174,34.341568");

        // 添加标记点(markers)
        StringBuilder markers = new StringBuilder("&markers=");
        char label = 'A';

        // 添加路径(paths)
        StringBuilder paths = new StringBuilder("&paths=5,0x0000ff,1,,");
        boolean firstPathPoint = true;

        for (Map.Entry<String, Coordinate> entry : coordinates.entrySet()) {
            Coordinate coord = entry.getValue();

            // 添加标记点 - 注意经纬度顺序：经度,纬度
            markers.append("mid,0xFF0000,")
                    .append(label++)
                    .append(":")
                    .append(coord.getLongitude())
                    .append(",")
                    .append(coord.getLatitude())
                    .append("|");

            // 添加路径点 - 注意经纬度顺序：经度,纬度
            if (firstPathPoint) {
                paths.append(":")
                        .append(coord.getLongitude())
                        .append(",")
                        .append(coord.getLatitude());
                firstPathPoint = false;
            } else {
                paths.append(";")
                        .append(coord.getLongitude())
                        .append(",")
                        .append(coord.getLatitude());
            }
        }

        // 处理多余的符号
        if (markers.length() > 0 && markers.charAt(markers.length() - 1) == '|') {
            markers.deleteCharAt(markers.length() - 1);
        }

        // 拼接完整URL
        urlBuilder.append(markers);
        // 只有当存在路径时才添加paths参数
        if (coordinates.size() > 1) {
            urlBuilder.append(paths);
        }

        return urlBuilder.toString();
    }
}